package com.carbohydrate42.kotlin_compose_demo.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Card
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.DismissDirection
import androidx.compose.material.DismissValue
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.SwipeToDismiss
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.rememberDismissState
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 * reference
 *
 * https://mobikul.com/swipe-to-dismiss-jetpack-compose/#:~:text=Compose%20provides%20a%20variety%20of%20APIs%20to%20help,move%20to%20cart%20and%20add%20to%20wishlist%2C%20etc.
 * https://medium.com/mobile-app-development-publication/jetpack-compose-swipe-to-dismiss-made-easy-323ca80a0355
 */
@ExperimentalMaterialApi
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun SwipeToDismissExt() {
    val dismissState = rememberDismissState(
        initialValue = DismissValue.Default,
        confirmStateChange = {
            when (it) {
                DismissValue.DismissedToEnd -> {
                    // Do Something when swipe Start To End
                    true
                }

                DismissValue.DismissedToStart -> {
                    // Do Something when swipe End To Start
                    true
                }

                else -> {
                    false
                }
            }
        }
    )


    Column {


        CircularProgressIndicator()

        SwipeToDismiss(
            state = dismissState,
            /***  create dismiss alert Background */
            background = {
                val color = when (dismissState.dismissDirection) {
                    DismissDirection.StartToEnd -> Color.Green
                    DismissDirection.EndToStart -> Color.Red
                    null -> Color.Transparent
                }
                val direction = dismissState.dismissDirection

                if (direction == DismissDirection.StartToEnd) {
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(color)
                            .padding(8.dp)
                    ) {
                        Column(modifier = Modifier.align(Alignment.CenterStart)) {
                            Icon(
                                imageVector = Icons.Default.ArrowForward,
                                contentDescription = null,
                                tint = Color.White,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            Text(
                                text = "swipe right", fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center,
                                color = Color.White
                            )
                        }

                    }
                } else {
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(color)
                            .padding(8.dp)
                    ) {
                        Column(modifier = Modifier.align(Alignment.CenterEnd)) {
                            Icon(
                                imageVector = Icons.Default.ArrowBack,
                                contentDescription = null,
                                tint = Color.White,
                                modifier = Modifier.align(Alignment.CenterHorizontally)
                            )
                            Spacer(modifier = Modifier.heightIn(5.dp))
                            Text(
                                text = "swipe left",
                                textAlign = TextAlign.Center,
                                fontWeight = FontWeight.Bold,
                                color = Color.LightGray
                            )

                        }
                    }
                }
            },
            /**** Dismiss Content */
            dismissContent = {
                Card(
                    backgroundColor = Color.White,
                    modifier = Modifier
                        .height(80.dp)
                        .fillMaxWidth()
                ) {
                    Text(text = "swipe left or right")
                }
            },
            /*** Set Direction to dismiss */
            directions = setOf(DismissDirection.EndToStart, DismissDirection.StartToEnd),
        )
    }


}